#include "Utils.h"

QRect Utils::fitRectToContainer(const QRect &container, const QRect &box)
{
    QRect rc;
    if (box.isEmpty()) return rc;

    int container_width = container.width();
    int container_height = container.height();

    qreal box_wh_ratio = box.width() * 1.0 / box.height();
    qreal container_wh_ratio = container_width  * 1.0 / container.height();
    if (box_wh_ratio > container_wh_ratio)
    {
        rc.setWidth(container_width);
        rc.setHeight(container_width / box_wh_ratio);
    }
    else if (box_wh_ratio < container_wh_ratio)
    {
        rc.setHeight(container_height);
        rc.setWidth(container_height * box_wh_ratio);
    }
    else
    {
        rc = container;
    }

    return rc;
}
